import { Base } from "../../utils/base.js";
import { Article } from "list-model.js";
var WxParse = require('../../utils/wxParse/wxParse.js');
import { Evaluate } from '../evaluate/evaluate-model.js';
var evaluate = new Evaluate();
var base= new Base();
var article = new Article();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShow:true,
    inputVal:"",
    page:1,
    loadingHidden: false,
    searchLoading: true,
    searchLoadingComplete: false,
    inputFocus:false,
    toComment:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var that = this;
    var params = {
      url: 'article/view?id=' + that.options.id,
      data: { },
      type: 'post',
      sCallback: function (data) {
        wx.setNavigationBarTitle({
          title: data.title
        })
        that.setData({
          info:data,
          loadingHidden:true,
        })
        WxParse.wxParse('content', 'html', data.data.content, that, 0);
        article.commentList(data.id, 1,(res) => {
          if (res._meta.pageCount == 1) {
            that.setData({
              searchLoading: false,
              searchLoadingComplete: true,
            })
          }
          that.setData({
            commentData: res.items,
            count: res._meta.totalCount,
            pageCount:res._meta.pageCount,
          })
          for (var i = 0; i < res.items.length; i++) {
            var date = evaluate.toDate(res.items[i].created_at);
            var time = 'commentData[' + i + '].created_at';
            that.setData({
              [time]: date,
            })
          }

        })
      }
    }

    base.request(params);

  },

  /**
  * 点赞
  */
  getVote: function () {
    var isVote = this.data.info.isVote;
    if (isVote == 0) {
      article.toVoteAdd(this.data.info.id, (res) => {
        var status = res.status;
        if(status==1){
          var voteCount = this.data.info.voteCount+1
          this.setData({
            'info.isVote': 1,
            'info.voteCount': voteCount
          })
          wx.showToast({
            title: '点赞成功~',
            icon: 'none',
            duration: 2000
          })
        }

      })
    } else if(isVote == 1){
      article.toVoteAdd(this.data.info.id, (res) => {
        var status = res.status;
        if(status==2){
          var voteCount = this.data.info.voteCount - 1
          this.setData({
            'info.isVote': 0,
            'info.voteCount': voteCount
          })
          wx.showToast({
            title: '点赞取消~',
            icon: 'none',
            duration: 2000
          })
        }
      }) 
    }

  },

  /**
  * 收藏
  */
  getFavourite:function(){
    var isFavourite = this.data.info.isFavourite;
    if (isFavourite == 0) {
      article.toFavouriteAdd(this.data.info.id, (res) => {
        var status = res.status;
        this.setData({
          'info.isFavourite': 1
        })
        wx.showToast({
          title: '收藏成功~',
          icon: 'none',
          duration: 2000
        })
      })
    } else if (isFavourite == 1) {
      article.toVoteAdd(this.data.info.id, (res) => {
        var status = res.status;
        this.setData({
          'info.isFavourite': 0
        })
        wx.showToast({
          title: '收藏取消~',
          icon: 'none',
          duration: 2000
        })
      })
    }

  },

  //发表评论
  inputTyping: function (e) {
    this.setData({
      inputVal: e.detail.value
    });
  },
  toInput: function () {
    this.setData({
      isShow: false,
      inputVal: "",
      inputFocus: true,
    })
  },
  sendComment:function(){
    var that = this;
    if (this.data.inputVal==''){
      wx.showToast({
        title: '请填写评论内容~',
        icon: 'none',
        duration: 2000
      })
    }
    article.tosendComment(this.data.info.id, this.data.inputVal,(data)=>{
      if(data.status==1){
        wx.showToast({
          title: '评论成功~',
          icon: 'none',
          duration: 2000
        })
        that.setData({
          isShow: true,
          'info.commentCount': that.data.info.commentCount + 1,
          searchLoadingComplete: false,
          page: 1,
          toComment:'commentList11',
        })
        article.commentList(that.data.info.id,1, (res) => {
          if (res._meta.pageCount == 1) {
            that.setData({
              searchLoading: false,
              searchLoadingComplete: true,
            })
          }
          that.setData({
            commentData: res.items,
          })
          for (var i = 0; i < res.items.length; i++) {
            var date = evaluate.toDate(res.items[i].created_at);
            var time = 'commentData[' + i + '].created_at';
            that.setData({
              [time]: date
            })
          }

        })
      }

    })
  },
  toClose:function(){
    this.setData({
      isShow:true
    })
  },

  //加载更多评论
  getMore: function () {
    var page = parseInt(this.data.page) + 1;
    if (page <= this.data.pageCount) {
      this.setData({
        searchLoading: true,
      })
      article.commentList(this.data.info.id,page, (res) => {
        this.scrollLoadData(res,page)
      })
    }
  },
  //拼接数据
  scrollLoadData: function (res, page) {
    var data = res.items;
    var commentData = this.data.commentData;
    for (var i = 0; i < data.length; i++) {
       data[i].created_at = evaluate.toDate(data[i].created_at);
    }
    commentData = commentData.concat(data)
    this.setData({
      commentData: commentData,
      page: page,
    })

    if (page == this.data.pageCount) {
      this.setData({
        searchLoading: false,
        searchLoadingComplete: true,
      })
    }
  },

  /**
* 跳转到首页
*/
  toHomeTab:function(){
    wx.switchTab({
      url: '../home/home',
    })
  },

  /**
* 用户点击右上角分享
*/
  onShareAppMessage: function () {
    console.log(this.data.info.title)
    return {
      title: this.data.info.title
    }
  }
})

 